﻿using Castle.MicroKernel.Registration;
using Castle.Windsor;
using DosTexPrj.Core.RepositoriesInterface;
using DosTexPrj.Implement.NHibernate.RepositoriesImp;
using NHibernate;
using NHibernate.Cfg;

namespace DosTexPrj.Implement.NHibernate
{
    public class BootTrapper
    {
        public static void BootAll()
        {
            WindsorContainer container = new WindsorContainer();

            container.Register(Component.For<ISessionFactory>()
                            .LifeStyle.Singleton
                            .UsingFactoryMethod(kernel => kernel.Resolve<Configuration>().BuildSessionFactory()));
            container.Register(Component.For<Configuration>()
                            .LifeStyle.Singleton
                            .UsingFactoryMethod(() => NHibernateUtility.BuildConfigurationMsSql()));

            container.Register(Component.For(typeof(IRepository))
                                    .ImplementedBy<Repository>()
                                    .Named("GenericRepository")
                                    .LifeStyle.PerThread);
            container.Register(Component.For(typeof(IRoleRepository))
                                    .ImplementedBy<RoleRepository>()
                                    .Named("RoleGenericRepository")
                                    .LifeStyle.PerThread);
            container.Register(Component.For(typeof(IUserRepository))
                                    .ImplementedBy<UserRepository>()
                                    .Named("UserGenericRepository")
                                    .LifeStyle.PerThread);

            container.Register(Component.For(typeof(ICustomerRepository))
                                    .ImplementedBy<CustomerRepository>()
                                    .Named("CustomerGenericRepository")
                                    .LifeStyle.PerThread);

            container.Register(Component.For(typeof(IOrderRepository))
                                    .ImplementedBy<OrderRepository>()
                                    .Named("OrderGenericRepository")
                                    .LifeStyle.PerThread);

            MyServiceLocator.SetContainer(container);
        }
    }
}